
Security News
ESLint Adds Official Support for Linting HTML
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
postcss-custom-media
Advanced tools
The postcss-custom-media package allows you to define custom media queries in CSS, which can then be reused throughout your stylesheets. This helps in maintaining a consistent and DRY (Don't Repeat Yourself) approach to media queries.
Define Custom Media Queries
This feature allows you to define custom media queries using the @custom-media rule. In this example, a custom media query named --small-viewport is defined for viewports with a maximum width of 30em.
@custom-media --small-viewport (max-width: 30em);
Use Custom Media Queries
Once a custom media query is defined, it can be used in your CSS just like any other media query. In this example, the custom media query --small-viewport is used to change the text color to red for small viewports.
body { color: black; }
@media (--small-viewport) { body { color: red; } }
postcss-preset-env allows you to use future CSS features today by converting modern CSS into something most browsers can understand. It includes a variety of plugins, including the ability to use custom media queries. Compared to postcss-custom-media, postcss-preset-env offers a broader range of features but may be overkill if you only need custom media queries.
postcss-media-minmax lets you write simpler and more concise media queries by using the < and > operators. While it doesn't offer custom media queries, it simplifies the syntax of media queries, making them easier to read and write. It is a good alternative if you are looking for a way to simplify your media queries without defining custom ones.
PostCSS Custom Media lets you define @custom-media
in CSS following the Custom Media Specification.
@custom-media --small-viewport (max-width: 30em);
@media (--small-viewport) {
/* styles for small viewport */
}
/* becomes */
@media (max-width: 30em) {
/* styles for small viewport */
}
Add PostCSS Custom Media to your project:
npm install postcss postcss-custom-media --save-dev
Use it as a PostCSS plugin:
const postcss = require('postcss');
const postcssCustomMedia = require('postcss-custom-media');
postcss([
postcssCustomMedia(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);
PostCSS Custom Media runs in all Node environments, with special instructions for:
Node | PostCSS CLI | Webpack | Create React App | Gulp | Grunt |
---|
The preserve
option determines whether the original notation
is preserved. By default, it is not preserved.
postcssCustomMedia({ preserve: true })
@custom-media --small-viewport (max-width: 30em);
@media (--small-viewport) {
/* styles for small viewport */
}
/* becomes */
@custom-media --small-viewport (max-width: 30em);
@media (max-width: 30em) {
/* styles for small viewport */
}
@media (--small-viewport) {
/* styles for small viewport */
}
The importFrom
option specifies sources where custom media can be imported
from, which might be CSS, JS, and JSON files, functions, and directly passed
objects.
postcssCustomMedia({
importFrom: 'path/to/file.css' // => @custom-selector --small-viewport (max-width: 30em);
});
@media (max-width: 30em) {
/* styles for small viewport */
}
@media (--small-viewport) {
/* styles for small viewport */
}
Multiple sources can be passed into this option, and they will be parsed in the
order they are received. JavaScript files, JSON files, functions, and objects
will need to namespace custom media using the customMedia
or
custom-media
key.
postcssCustomMedia({
importFrom: [
'path/to/file.css',
'and/then/this.js',
'and/then/that.json',
{
customMedia: { '--small-viewport': '(max-width: 30em)' }
},
() => {
const customMedia = { '--small-viewport': '(max-width: 30em)' };
return { customMedia };
}
]
});
The exportTo
option specifies destinations where custom media can be exported
to, which might be CSS, JS, and JSON files, functions, and directly passed
objects.
postcssCustomMedia({
exportTo: 'path/to/file.css' // @custom-media --small-viewport (max-width: 30em);
});
Multiple destinations can be passed into this option, and they will be parsed
in the order they are received. JavaScript files, JSON files, and objects will
need to namespace custom media using the customMedia
or
custom-media
key.
const cachedObject = { customMedia: {} };
postcssCustomMedia({
exportTo: [
'path/to/file.css', // @custom-media --small-viewport (max-width: 30em);
'and/then/this.js', // module.exports = { customMedia: { '--small-viewport': '(max-width: 30em)' } }
'and/then/this.mjs', // export const customMedia = { '--small-viewport': '(max-width: 30em)' } }
'and/then/that.json', // { "custom-media": { "--small-viewport": "(max-width: 30em)" } }
cachedObject,
customMedia => {
customMedia // { '--small-viewport': '(max-width: 30em)' }
}
]
});
FAQs
Use Custom Media Queries in CSS
The npm package postcss-custom-media receives a total of 4,982,757 weekly downloads. As such, postcss-custom-media popularity was classified as popular.
We found that postcss-custom-media demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.